<template>
	<view>
		<radio-group v-if="addressList.length > 0" class="radio-group" @change="radioChange">
		    <view class='item' v-for="(item, index) in addressList" v-key="index">
		        <view class='address' @click='goOrder(index)'>
		          <view class='consignee'>收货人：{{item.name}}<text class='phone'>{{item.mobile}}</text></view>
		          <view>收货地址：{{item.province}}{{item.city}}{{item.region}}{{item.detail}}</view>
		        </view>
		        <view class='operation'>
		            <radio class="left" :value="index" :checked="item.is_default ? true : false">
		              <text>设为默认</text>
		            </radio>
		            <view class='right'>
		              <view @click='editAddress(item.id)'><text class='iconfont icon-bianji'></text>编辑</view>
		              <view @click='delAddress(item.id)'><text class='iconfont icon-shanchu'></text>删除</view>
		            </view>
		        </view>
		    </view>
		</radio-group>
		<view class='noCommodity' v-else>
			<view class='pictrue'><image src='/static/noAddress.png'></image></view>
		</view>
		
		<view class="navigation">
		    <view class="item">
		      <view class="left" @click="getWxAddress">一键导入微信地址</view>
		    </view>
		    <view class="item">
		      <view class="right" @click="addAddress">新增地址</view>
		    </view>
		</view>
	</view>
</template>

<script>
	import { Address } from '@/utils/model/address-model.js';
	var address = new Address();
	
	export default{
		data(){
			return{
				addressList: [],
				current: 0
			}
		},
		onShow() {
			this.getList()
		},
		onLoad() {
			//this.getList();
		},
		methods:{
			
			getWxAddress(event) {
			    var that = this;
			    wx.authorize({
			      scope: 'scope.address',
			      success: function (res) {
			        wx.chooseAddress({
			          success: function (res) {
			            var addressInfo = {
			              name: res.userName,
			              mobile: res.telNumber,
			              province: res.provinceName,
			              city: res.cityName,
			              country: '中国',
			              region: res.countyName,//地区
			              detail: res.detailInfo,
			            };
			            console.log(addressInfo)
			            //新增地址
			            address.add(addressInfo, (ress) => {
			              console.log(ress);
			              console.log('aaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
			              that.getList()
			            });
			          },
			          fail: function (res) {
			            if (res.errMsg == 'chooseAddress:cancel') return app.Tips({ title:'取消选择'});
			          },
			        })
			      },
			      fail: function (res) {
			        wx.showModal({
			          title: '您已拒绝导入微信地址权限',
			          content:'是否进入权限管理，调整授权？',
			          success(res) {
			            if (res.confirm) {
			              wx.openSetting({
			                success: function (res) {
			                  console.log(res.authSetting)
			                }
			              });
			            } else if (res.cancel){
			              return app.Tips({ title: '已取消！' });
			            }
			          }
			        })
			      },
			    })
			  },
			
			
			
			getList(){
				address.getList().then(res => {
					console.log(res)
					if(res.code == 1){
						this.addressList = res.data.list
					}
				}).catch(err => {
					console.log('eeeeeeeeeeeeeeeeeeeeeeeeeeeeee')
					console.log(err)
				})
				
			},
			addAddress(){
				uni.navigateTo({
					url: 'add'
				})
			},
			editAddress(id){
			    uni.navigateTo({
			      url: 'edit?id=' + id,
			    })
			  },
			radioChange(e){
				let index = parseInt(e.detail.value);
				
				var addressInfo = this.addressList[index];
				if (addressInfo == undefined){
					uni.showToast({
						title: '您设置的默认地址不存在!',
						icon: 'none'
					})
					return
				}
				let id = addressInfo.id;
				this.setDefault(id)
			},
			setDefault(id){
				address.setDefault(id).then((res) => {
					console.log(res)
					if(res.code == 1){
						this.getList()
						 /* for (var i = 0, len = that.data.addressList.length; i < len; i++) {
							if (i == index) that.data.addressList[i].is_default = true;
							else that.data.addressList[i].is_default = false;
						  }
						  wx.showToast({
							title: '设置成功!',
							icon: 'success'
						  })
						  that.setData({ addressList: that.data.addressList }); */
					}
				}).catch((err) => {
					console.log(err)
					
				})
			},
			delAddress(id){
				address.delete(id).then((res) => {
					console.log(res)
					if(res.code == 1){
						//this.addressList = res.result.addressList
						this.getList()
					}
				}).catch((err) => {
					console.log(err)
					
				})
			}
		}
	}
</script>
 
<style>
	page{
		background-color: #F5F5F5;
	}
</style>
 
<style scoped lang="scss">
	@import '~@/pages/my/address/index.scss';
</style>
